home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 1 of 3 / CHAPTE24 / EX4.C < prev   
C/C++ Source or Header  |  1995-07-04  |  2KB  |  57 lines

  1. #include <genstub.c>
  2.  
  3. #define TIMER1  1     // Callback timer.
  4. #define TIMER2  2     // WM_TIMER timer.
  5.  
  6. HWND hWndGlobal = 0;  // Main window's handle.
  7.  
  8. // Processes events received by the callback funtion
  9. VOID CALLBACK TimerProc( HWND hWnd, UINT uMsg, UINT idEvent, DWORD dwTime )
  10. {
  11.    TCHAR szBuffer[128];
  12.    static int times = 0;
  13.    HDC hDC = GetDC( hWndGlobal );
  14.    wsprintf( szBuffer, "TIMER1 received %d times.  ", ++times );
  15.    TextOut( hDC, 0, 0, szBuffer, lstrlen(szBuffer ) );
  16.    ReleaseDC( hWndGlobal, hDC );
  17. }
  18.  
  19. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  20. {
  21.    switch (uMsg)
  22.    {
  23.        case WM_CREATE:       // Store window handle in global for timer callback.
  24.                hWndGlobal = hWnd;
  25.                return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  26.        case WM_COMMAND:      // Process menu items.
  27.                switch ( LOWORD( wParam ) )
  28.                {
  29.                    case IDM_TEST:  // Set up two timers, one callback timer
  30.                    {               // and one message-based timer.
  31.                       SetTimer( hWnd, TIMER1, 500, (TIMERPROC) TimerProc );
  32.                       SetTimer( hWnd, TIMER2, 1000, NULL );
  33.                    }
  34.                    break;
  35.                    case IDM_EXIT:
  36.                       DestroyWindow( hWnd );
  37.                       break;
  38.                }
  39.                break;
  40.        case WM_TIMER:  // Process messages received by the message-based timer.
  41.                {
  42.                   TCHAR szBuffer[128];
  43.                   static int times = 0;
  44.                   HDC hDC = GetDC( hWnd );
  45.                   wsprintf( szBuffer, "TIMER2 received %d times.  ", ++times );
  46.                   TextOut( hDC, 0, 20, szBuffer, lstrlen(szBuffer ) );
  47.                   ReleaseDC( hWnd, hDC );
  48.                }
  49.                return 0;
  50.        case WM_DESTROY:
  51.                PostQuitMessage( 0 );
  52.                break;
  53.        default:
  54.                return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  55.    }
  56.    return (NULL);
  57. }